<?php

include_once '../info_struct.php';
include_once 'evaluate.php';

/*
 * Usage:
 * complete_data(ref);
 * Parameter:
 * ref - infos object that is to be completed before adding it to the
 *     local database
 * Return:
 * The completed infos object.
 */
function complete_data($ref) {
    $source = $ref->from;
    
    switch ($source) {
        case "doi":
            $ret = complete_doi($ref);
            break;
        case "zma":
            $ret = complete_zmath($ref);
            break;
        case "arx":
            $ret = complete_arxiv($ref);
            break;
        default:
            $ret = $ref;
    }
    return $ret;
}

/*
 * Usage:
 * complete_doi(ref);
 * Helper function for complete_data
 * Parameter:
 * ref - infos object obtained from the DOI (CrossRef) database
 * Return:
 * infos object that contains information from all external databases
 * (if additional information is available)
 */
function complete_doi($ref) {
    /* String for searching the other databases */
    $search_string = join(" ", array($ref->title, $ref->author, $ref->year));
    /* Get results from the other databases */
    $res_zmath = zmath_get_by_string($search_string, "any:");
    $res_arxiv = arxiv_get_by_string($search_string);
    /* Initialize ret-list */
    $ret = array();
    /* If there are any results add them to the ret-list */
    if ($res_zmath) {
        $ret = $res_zmath;
    }
    if ($res_arxiv) {
        $ret = array_merge($ret, $res_arxiv);
    }
    /* Check ret-list for possible matches */
    $ret = evaluate_infos($ref, $ret);
    
    return $ret;
}

/*
 * Usage:
 * complete_zmath(ref);
 * Parameter:
 * ref - infos object obtained from the ZMath database
 * Return:
 * infos object containing information from all external databases
 * (if additional information is available)
 */
function complete_zmath($ref) {
    /* For further comments check the function above */
    $search_string = join(" ", array($ref->title, $ref->author, $ref->year));
    
    /* If there is a DOI given then get the DOI-result by id, else by search */
    If ($ref->doi) {
        $res_doi = array(doi_get_by_id($ref->doi));
    } else {
        $res_doi = doi_get_by_string($search_string);
    }
    $res_arxiv = arxiv_get_by_string($search_string);
    $ret = array();
    if ($res_doi) {
        $ret = $res_doi;
    }
    if ($res_arxiv) {
        $ret = array_merge($ret, $res_arxiv);
    }

    $ret = evaluate_infos($ref, $ret);
    
    return $ret;
}

/*
 * Usage:
 * complete_arxiv(ref);
 * Parameter:
 * ref - infos object obtained from the ArXiv database
 * Return:
 * infos object containing data from all external databases
 * (if additional information is available)
 */
function complete_arxiv($ref) {
    /* For further comments check the functions above */
    $search_string = join(" ", array($ref->title, $ref->author, $ref->year));
    If ($ref->doi) {
        $res_doi = array(doi_get_by_id($ref->doi));
    } else {
        $res_doi = doi_get_by_string($search_string);
    }
    $res_zmath = zmath_get_by_string($search_string, "any:");
    $ret = array();
    if ($res_doi) {
        $ret = $res_doi;
    }
    if ($res_zmath) {
        $ret = array_merge($ret, $res_zmath);
    }

    $ret = evaluate_infos($ref, $ret);
    
    return $ret;
}

/*
 * Usage:
 * comp(in,refs);
 * Parameter:
 * in - infos object that is to be completed with additional information
 * refs - Array of infos objects that might contain additional information
 * Return:
 * infos object with addition information (if available) compared to the initial
 * infos object
 * 
 * IMPORTANT: Currently not in use in the system
 */
function comp($in, $refs) {
    $ret = $in;
    
    foreach ($refs as $ref) {
        if ($ret->title == $ref->title && $ret->author == $ref->author
                && $ret->year == $ref->year) {
            $ret = comp_add_data($ret, $ref);
        }
    }
    
    return $ret;
}

/*
 * Usage:
 * comp_add_data(ref, ad)
 * Parameter:
 * ref - infos object that is to be completed
 * ad - infos object containing additional information
 * Return:
 * The function changes the parameter ref and adds information
 * to it.
 * 
 * IMPORTANT: Currently not in use in the system
 */
function comp_add_data($ref, $ad) {
    if (!$ref->doi) {
        $ref->doi = $ad->doi;
    }
    if (!$ref->zmath) {
        $ref->zmath = $ad->zmath;
    }
    if (!$ref->arxiv) {
        $ref->arxiv = $ad->arxiv;
    }
    if (!$ref->journal) {
        $ref->journal = $ad->journal;
    }
    if (!$ref->volume) {
        $ref->volume = $ad->volume;
    }
    if (!$ref->number) {
        $ref->number = $ad->number;
    }
    if (!$ref->pages) {
        $ref->pages = $ad->pages;
    }
    if (!$ref->other) {
        $ref->other = $ad->other;
    }
    
    return $ref;
}